package com.serve.oa.controller;

import lombok.Data;


import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;



// 测试类
public class Ssyn {
    static Random random = new Random();

    public static void main(String[] args) {
        String s1 = "8四川凉山突发森林大火？谣言";
        System.out.println(Arrays.toString(s1.toLowerCase().split("\\W+")));
        InventoryService service = new InventoryService();
        service.initializeInventory();


        float f = 0.44F;
        double d = 0.213122321131;
        long l = 213;
        boolean b= false;
        char c = '\n';
        int sd = 22;
        byte by = 111;
        short s = (short) 222222;
        System.out.println(s);

    }
}

// 商品类
@Data
class Product {
    private int productId;
    private String name;
    private int stock;

    public Product(int productId, String name, int stock) {
        String[] stringArr = {"hello", "world", "!"};
        this.productId = productId;
        this.name = name;
        this.stock = stock;
    }

    // 商品ID的getter和setter
    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    // 名称的getter和setter
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // 库存的getter和setter
    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

}

// 自定义库存不足异常
class StockInsufficientException extends Exception {
    public StockInsufficientException(String message) {
        super(message);
    }
}

// 库存服务类
class InventoryService {
    private Map<Integer, Product> products = new HashMap<>();
    private final Lock lock = new ReentrantLock(); // 使用ReentrantLock

    // -
    public void deductStock(int productId, int quantity) throws StockInsufficientException {

        lock.lock();
        try {
            Integer nb =  products.get(productId).getStock();
            products.get(productId).setStock(nb - quantity);
            System.out.println("商品"+productId+ "减去" + quantity+ " ------- "+products);
        }catch (Exception e){
            throw new StockInsufficientException("2");
        }finally {
            lock.unlock();
        }

    }
    // +
    public void replenishStock(int productId, int quantity)throws StockInsufficientException {
        lock.lock();
        try {
            Integer nb =  products.get(productId).getStock();
            products.get(productId).setStock(nb + quantity);
            System.out.println("商品"+productId+ "加上" + quantity+ " ------- "+products);
        }catch (Exception e){
            throw new StockInsufficientException("2");
        }finally {
            lock.unlock();
        }
    }

    // 初始化库存
    public void initializeInventory() {
        // 示例初始化一些商品库存
        products.put(1, new Product(1, "商品1", 100));
        products.put(2, new Product(2, "商品2", 200));
        products.put(3, new Product(3, "商品3", 300));
        products.put(4, new Product(4, "商品4", 400));
        System.out.println("初始----------"+products);
        // ...其他商品
    }
}

